class CommentsController < ApplicationController

  before_action :authenticate_user!

  def create
    @comment = Comment.new comment_attrs
    @comment.user = current_user
    if @comment.save
      blogsComments = BlogsComments.new(:blog_id => @comment.blog_id, :comment_id => @comment.id)
      if blogsComments.save
        flash[:notice] = "评论关联成功"
      else
        flash[:notice] = "评论关联失败"
      end
      flash[:notice] = "评论发布成功"
      redirect_to blog_path(params[:blog_id])
    else
      flash[:notice] = "评论发布失败"
      redirect_to blog_path(params[:blog_id])
    end
  end

  private

  def comment_attrs
    params.permit(:content, :blog_id)
  end
end
